<?php
namespace app\admin\controller;
// use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
use think\captcha\Captcha;
use \app\admin\model\Users as m;
class User extends Common
{

    public function index()
    {
        Cookie::set('Jumpurl',$_SERVER['REQUEST_URI']);
        // 控制器模块
        // $this->assign('controller',Request()->controller());
        // $this->assign('module',Request()->module());
        // $this->assign('action',Request()->action());
        
        $pagesize=input('pagesize',0);
        $q = input('q',0);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        $vs['listurl']=url('user/index');
        $vs['addurl']=url('user/add');
        $vs['catname']='个人中心';

        if($q) $map['username']=array('like',"%$q%");
        $list=Db::name("user")->where($map)->order("sendtime desc,id desc")->paginate($pagesizes);

        $this->assign("list",$list);
        $this->assign("vs",$vs);
        return $this->fetch("/user/index");
    }


    //添加
    public function add()

    {
        $url = $_SERVER['REQUEST_URI'];
        $db = Db::name('user');
        $id=input('id',0);
        
        $v['listurl']=url('user/index');
        $v['addurl']=url('user/add');
        $v['catname']='个人中心';
        if($id){
            $v=Db::name("user")->find($id);
            $v['listurl']=url('user/index');
            $v['addurl']=url('user/add');
            $v['catname']="个人中心";
        }
        $this->assign("v",$v);
        
        if(Request::instance()->isPost()){
            $data=input('post.');
            // print_r($data);die;
            if($id && $v['password'] != $data['password']){
                
                if(!empty($data['password'])){
                    $data['password']=md5(input('post.password'));
                }else{
                    unset($data['password']);
                }
                if(!empty($data['repassword'])){
                    $data['repassword']=md5(input('post.repassword'));
                }else{
                    unset($data['repassword']);
                }
            }
            if(empty($data['password'])){
                $this->error("请输入密码！！！");
                exit();
            }
            if(empty($data['repassword'])){
                $this->error("请输入确认密码！！！");
                exit();
            }
            if($data['password'] != $data['repassword']){
                $this->error("密码不一致！！！");
                exit();
            }
            
            unset($data['repassword']);
            $data['sendtime']=time();
            if($id){
                if($db->update($data) !== false){
                    // $this->redirect("编辑成功",Cookie::get('Jumpurl'));
                    $this->success("修改成功",Cookie::get('Jumpurl'));
                    exit();
                }
            }else{
                $username=$data['username'];
                $count=$db->where("username='$username'")->count();
                if($count){
                    $this->error("用户名已存在,请重新输入！！！");
                    exit();
                }
                if($id=$db->insert($data)){
                    //$this->redirect("添加成功",Cookie::get('Jumpurl'));
                    $this->success('添加成功',Cookie::get('Jumpurl'));
                    exit();
                }
            }
        }
        return $this->fetch("/user/add");
    }
    //删除
    public function del()
    {
        $id=input('id',0);
        $v=Db::name("user")->find($id);
        if(Db::name("user")->delete($id)){
            $url="/admin/user/index";
            if($v['pid'])$url.="/pid/".$v['pid'];
            if($v['ty'])$url.="/ty/".$v['ty'];
            if($v['tty'])$url.="/tty/".$v['tty'];
            if($v['ttty'])$url.="/ttty/".$v['ttty'];
            if(file_exists(substr($v['img1'],1))){
                unlink(substr($v['img1'],1));
            }
            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",$url);
        }else{
            $this->error("删除失败");
        }

    }


    public function action()
    {
        $url=$_SERVER['HTTP_REFERER'];
        $act=$_POST['act'];
        $sorting_arr=$_POST['sorting'];
        $checkid=$_POST['checkid'];
        // print_r($checkid);
        // print_r($_POST);
        // die;
        if($act=='sorting'){
            for($i=0;$i<count($checkid);$i++) {
                Db()->execute("update ".config('database.prefix')."user set disorder=$sorting_arr[$i] where id='{$checkid[$i]}'");
                $this->sys_log("信息排序更新:(id:$checkid[$i])");
            }
            $this->success("信息排序更新成功",$url);
        }elseif($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {
                $this->sys_log("删除信息:(id:$checkid[$i])");
                Db::name("user")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",$url);
        }elseif($act=='isgood'||$act=='istop') {
            for($i=0;$i<count($checkid);$i++) {
                $v=Db::name("user")->find($checkid[$i]);
                $data[$act]=$v[$act]?0:1;
                Db::name("user")->where("id=".$checkid[$i])->update($data);
               $this->sys_log("删除信息:(id:$checkid[$i])");
            }
            $this->success("操作成功",$url);
        }

    }

    //审核
    public function audit()
    {
        $url=$_SERVER['HTTP_REFERER'];
        $id=input('id');
        $zd='status';
        //print_r($_POST);
        $v=Db::name("user")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("user")->where("id=".$id)->update($data)){
           $this->success("审核成功",$url);
            // $return_data['status'] = 0;
            // $return_data['msg'] ="审核成功";
        }else{
           $this->error("审核失败",$url);
            // $return_data['status'] = 1;
            // $return_data['msg'] ="审核失败";
        }
        // echo json_encode($return_data);

    }


    //添加
    public function single()
    {
        $id=input('id',0);
        $pid=input('pid',0);
        $ty=input('ty',0);
        $tty=input('tty',0);
        $ttty=input('ttty',0);

        if($id){
            $v=Db::name("user")->find($id);
            $pid=$v['pid'];
            $ty=$v['ty'];
            $tty=$v['tty'];
            $ttty=$v['ttty'];
        }
        $url="/admin/user/single";

        if($ttty>0){
            $zid=$ttty;
        }elseif($tty>0){
            $zid=$tty;
        }elseif($ty>0){
            $zid=$ty;
        }elseif($pid>0){
            $zid=$pid;
        }

        if($pid){$url.="/pid/".$pid;$map['pid']=$pid;}
        if($ty){$url.="/ty/".$ty;$map['ty']=$ty;}
        if($tty){$url.="/tty/".$tty;$map['tty']=$tty;}
        if($ttty){$url.="/ttty/".$ttty;$map['ttty']=$ttty;}
        $url.=".html";
        $v=Db::name("user")->where($map)->find();

        if($v['id']){
            $v['pid']=$v['pid'];
            $v['ty']=$v['ty'];
            $v['tty']=$v['tty'];
            $v['ttty']=$v['ttty'];
            $v['ttyt']=$v['ttty'];
        }else{
            $v['pid']=$pid;
            $v['ty']=$ty;
            $v['tty']=$tty;
            $v['ttty']=$ttty;
            $v['id']=0;
            $v['title']="";
            $v['ftitle']="";
            $v['linkurl']="";
            $v['introduce']="";
            $v['seotitle']="";
            $v['seokeywords']="";
            $v['seodescription']="";
            $v['content']="";
            $v['sort_a']="";
            $v['imgnum']=0;
            $v['imgsize']="";
            $v['file1']="";
            $v['source']="";
            $v['author']="";
            $v['price']="";
            $v['hits']=0;
            $v['img1']="";
            $v['img2']="";
            $v['status']=1;
            $v['model_id']=1;
            $v['pid']=$pid;

            $v['disorder']=0;
            $v['sendtime']=time();

        }

        $v['catname']=get_sort_zd($zid);
        if(Request::instance()->isPost()){
            $data=input('post.');
            
            $data['sendtime']=strtotime($_POST['sendtime']);
            if($v['id']){
                if(Db::name("user")->update($data)){


                    $this->sys_log("编辑{$v['catname']}栏目内容:{$v['username']}(id:$id)");
                    $this->success("修改成功",$url);
                }else{
                    $this->error("修改失败");
                }
            }else{
                $data['sendtime']=time();
                if(Db::name("user")->insert($data)){
                    $this->success("添加成功",$url);
                }else{
                    $this->error("添加失败");
                }
            }
        }
        $v['imgnum']=get_sort_zd($zid,'imgnum')+1;
        //die;
        $model_id=get_sort_zd($zid,'model_id');//获取当前栏目模块id
        $model_fields=get_model_zd($model_id,'model_fields');
        $arr=explode(",",$model_fields);


        $this->assign("arr",$arr);
        $this->assign("v",$v);


        return $this->fetch("/user/single");
    }

    
    //上传图片
    public function uploads()
    {

        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $path=ROOT_PATH . 'public' . DS . 'uploads/images';
            $info = $file->rule('date')->move($path);
            //print_r($info);
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $return_data['status'] = 0;
                $path1= '/uploads/images';
                $return_data['msg'] =$path1.'/'.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }

            echo json_encode($return_data);

        }
    }
}